package com.taptech.doufu.ui.view.userdetail

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.google.gson.Gson
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.model.user.ListItem
import com.taptech.doufu.model.user.MedalListBean
import com.taptech.doufu.model.user.SumInfo
import com.taptech.doufu.services.personalcenter.AccountService
import com.taptech.doufu.ui.activity.BrowseActivity
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.GsonUtil
import kotlinx.android.synthetic.main.view_user_detail_medal.view.*
import kotlinx.android.synthetic.main.view_user_post_sum_info.view.*

class UserDetailMedalView(context: Context):LinearLayout(context) {


    init {
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.view_user_detail_medal,null)
        addView(view)
    }

    fun setData(uid:Int,data: ListItem) {
        if (AccountService.getInstance().isLogin && uid == AccountService.getInstance().userUid.toInt()) {
            tvTitle.text = "我的勋章"
        }else{
            tvTitle.text = "TA的勋章"
        }
        try {
            val info = GsonUtil.parseJsonWithGson(Gson().toJson(data.data), MedalListBean::class.java)
            tvCount.text = "(${info.count}枚)"
            val list = info.list
            if (list != null) {
                if (list.size > 0) {
                    ivMedal1.visibility = View.VISIBLE
                    GlideUtil.displayCircleImage(context,ivMedal1,list[0].url_l)
                }else{
                    ivMedal1.visibility = View.GONE
                }

                if (list.size > 1) {
                    ivMedal2.visibility = View.VISIBLE
                    GlideUtil.displayCircleImage(context,ivMedal2,list[1].url_l)
                }else{
                    ivMedal2.visibility = View.GONE
                }

                if (list.size > 2) {
                    ivMedal3.visibility = View.VISIBLE
                    GlideUtil.displayCircleImage(context,ivMedal3,list[2].url_l)
                }else{
                    ivMedal3.visibility = View.GONE
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        setOnClickListener {
            val intent = Intent(context,BrowseActivity::class.java)
            if (AccountService.getInstance().isLogin && uid == AccountService.getInstance().userUid.toInt()){
                intent.putExtra(Constant.URL,"https://api.doufu.la/myMedal/index")
            }else{
                intent.putExtra(Constant.URL,"https://api.doufu.la/myMedal/othersMedal?uid=$uid")
            }
            context.startActivity(intent)
        }
    }
}
